﻿#region
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using EndlessDownloader.Engine;

#endregion

namespace EndlessDownloader
{
    public partial class PageViewer : UserControl
    {
        private List<PictureViewer> _pictureViewers;

        public PageViewer()
        {
            InitializeComponent();
        }

        public void ShowLoadingLabel()
        {
            lblLoading.Visible = true;
            pnlPanel.Controls.Clear();
        }

        public void Initialize(List<Picture> pictures)
        {
            lblLoading.Visible = false;

            _pictureViewers = new List<PictureViewer>(pictures.Count);

            foreach (var imageUrl in pictures)
                _pictureViewers.Add(CreateFileDownloader(imageUrl));

            _pictureViewers.Reverse();
            foreach (var control in _pictureViewers)
                pnlPanel.Controls.Add(control);
        }

        public void SetAsCompleted(int downloadIndex)
        {
            PictureViewer pictureViewer = _pictureViewers.Find(x => x.Picture.DownloadIndex == downloadIndex);

            if (pictureViewer == null)
                return;

            pictureViewer.SetAsCompleted();
        }

        private PictureViewer CreateFileDownloader(Picture picture)
        {
            PictureViewer control = new PictureViewer();
            control.Initialize(picture);
            control.Dock = DockStyle.Top;

            return control;
        }
    }
}